import sys, pygame, random, time
from settings import SCREEN_SETTINGS, GAME_SETTINGS, BOARD_SETTINGS
from pygame.locals import *

from game.game import Game
from layers import board
from objects.animals.animal import Animal
from objects.plants.plant import Plant
from util.events.eventmanager import EventManager
import util.events.event as ev

def main():
    pygame.init()
    pygame.font.init()
    pygame.display.set_caption("pyLife")
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode(SCREEN_SETTINGS.SCREEN_SIZE)
    
    rootEventManager = EventManager()   
    game = Game(rootEventManager)
    game.run()
    
    numPlants = random.randint(1,20)
    numAnimals = random.randint(1,20)
    for x in range(numPlants):
        try:
            game.addEntity(random.randint(0,BOARD_SETTINGS.MAP_SIZE[0]),random.randint(0,BOARD_SETTINGS.MAP_SIZE[1]),Plant())
        except board.CoordinateOutOfBoundsException:
            pass
        except:
            raise
    for y in range(numAnimals):
        try:
            game.addEntity(random.randint(0,BOARD_SETTINGS.MAP_SIZE[0]),random.randint(0,BOARD_SETTINGS.MAP_SIZE[1]),Animal())
        except board.CoordinateOutOfBoundsException:
            pass
        except:
            raise

    while 1:
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == QUIT:
                return
        if game.running:
            game.doTurn()
            #pygame.time.wait(GAME_SETTINGS.GAME_SPEED)
            clock.tick(20)
            rootEventManager.Post(ev.TickEvent)
        
        
#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()